package com.red.wood.module.task;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.red.wood.common.util.CommonUtils;
import com.red.wood.common.util.DateUtil;
import com.red.wood.common.util.JdbcUtil;
import com.red.wood.common.util.SqlParser;
import com.red.wood.module.model.SqlInfo;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class StatisticsTask {
	
	private static final Log log = LogFactory.getLog(StatisticsTask.class);
	
	public static void main(String[] args) {
		Connection isdmsOracleConn = null;
		Connection isdmsMysqlConn = null;
		SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyyMMdd");
		try {
			String day = "";
			if(args.length > 0) {
				day = args[0];
				if(!isValidDate(day)) {
					System.exit(0);
				}
			}else {
				day = yyyyMMdd.format(DateUtil.getYesterday());
			}
			log.info("=========数据汇总开始==========");
			log.info("查询日期："+day);
			SqlInfo sqlInfo = getSqlInfo();
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String query_day = sdf.format(yyyyMMdd.parse(day));
			
			isdmsMysqlConn = JdbcUtil.getConnection("isdmsMysql");
			
			//实际入库时间
			String yesterday = yyyyMMdd.format(DateUtil.getYesterday());
			String sqlBasicResSt = SqlParser.getSql(sqlInfo.getSqlBasicResSt(), "day", yesterday);
			sqlBasicResSt = SqlParser.getSql(sqlBasicResSt, "query_day", query_day+"%");
			
			String query_day_before2 = sdf.format(DateUtil.getDayBeforeYesterday());
			List<Map<String,Object>> resList = getList(isdmsMysqlConn,sqlBasicResSt);
			for(Map<String,Object> map : resList) {
				String count = CommonUtils.valueOf(map.get("count"));
				if(StringUtils.isBlank(count)) {//昨天没数据跑前天的数据
					getDayBefore2(isdmsMysqlConn, sqlInfo, query_day_before2, map);
				}
			}
			log.info("基础资源数量："+resList);
			
			String sqlIcpSize = SqlParser.getSql(sqlInfo.getSqlIcpSize(), "day", yesterday);
			sqlIcpSize = SqlParser.getSql(sqlIcpSize, "query_day", query_day+"%");
			
			isdmsOracleConn = JdbcUtil.getConnection("isdmsOracle");
			String icpResCount = getCount(isdmsOracleConn,sqlInfo.getSqlIcpCount());
			log.info("备案资源数据量："+icpResCount);
			
			List<Map<String,Object>> icpList = getList(isdmsMysqlConn,sqlIcpSize);
			log.info("备案存储量："+icpList);
			if(icpList != null && icpList.size() > 0) {
				icpList.get(0).put("count", icpResCount);
			}else {
				icpList = new ArrayList<Map<String,Object>>();
				int file_size = 0;
				if(!StringUtils.equals(icpResCount, "0")) {
					file_size = 112;
				}
				Map<String,Object> map = new HashMap<String,Object>();
				map.put("day", yesterday);
				map.put("source_type","备案");
				map.put("source_name","备案");
				map.put("file_size", file_size);
				map.put("count", icpResCount);
				icpList.add(map);
			}
			resList.addAll(icpList);
			
			//yesterday = "20191207";
			//query_day = "2019-12-07";
			
			//统计各监控数据
			String sqlDataMonitor = SqlParser.getSql(sqlInfo.getSqlDataMonitorSt(), "day", yesterday);
			sqlDataMonitor = SqlParser.getSql(sqlDataMonitor, new String[] {"day","query_day","query_day_ymd"}, new Object[] {yesterday,query_day+"%",yesterday+"%"});

			List<Map<String,Object>> dataMonitorList = getList(isdmsMysqlConn,sqlDataMonitor);
			log.info("数据监控昨天数据："+dataMonitorList);
			
			String dayBeforeYesterday = yyyyMMdd.format(DateUtil.getDayBeforeYesterday());
			String queryDayBeforeYesterday = sdf.format(DateUtil.getDayBeforeYesterday());
			//dayBeforeYesterday="20200202";
			//queryDayBeforeYesterday="2020-02-02";
			sqlDataMonitor = SqlParser.getSql(sqlInfo.getSqlDataMonitorSt(), "day", dayBeforeYesterday);
			sqlDataMonitor = SqlParser.getSql(sqlDataMonitor, new String[] {"day","query_day","query_day_ymd"}, new Object[] {dayBeforeYesterday,queryDayBeforeYesterday+"%",dayBeforeYesterday+"%"});

			List<Map<String,Object>> dbyDataMonitorList = getList(isdmsMysqlConn,sqlDataMonitor);
			log.info("数据监控前天数据："+dbyDataMonitorList);
			//删除昨天各监控数据
			String delMonitorSql ="delete from t_st_source_monitor where day in ("+yesterday+","+dayBeforeYesterday+")";
			JdbcUtil.execSql(isdmsOracleConn, delMonitorSql);
			
			dataMonitorList.addAll(dbyDataMonitorList);
			String insertSql = "insert into t_st_source_monitor(source_type,source_name,source_count,day) values (?,?,?,?)";
			String columns = "source_type,source_name,source_count,day";
			JdbcUtil.insertBatchTableData(isdmsOracleConn, dataMonitorList, insertSql, columns, 500);
			
			//统计今日数据
			day = yyyyMMdd.format(new Date());
			query_day = sdf.format(yyyyMMdd.parse(day));
			String sqlCurrData = SqlParser.getSql(sqlInfo.getSqlCurrData(), "day", day);
			sqlCurrData = SqlParser.getSql(sqlCurrData, "query_day", query_day+"%");
			List<Map<String,Object>> currList = getList(isdmsMysqlConn,sqlCurrData);
			log.info("今日实时数据："+currList);
			
			resList.addAll(currList);
			
			//删除今天统计数据
			String delSql ="delete from t_st_source_statistics where day = "+day+" and source_type not in('工业互联','网安')";
			JdbcUtil.execSql(isdmsOracleConn, delSql);
			
			//删除昨天统计数据
			delSql ="delete from t_st_source_statistics where day="+yesterday+" and source_type not in('工业互联','网安')";
			JdbcUtil.execSql(isdmsOracleConn, delSql);
			
//			delSql = "delete from t_st_source_statistics where day in("+day+","+yesterday+")";
//			JdbcUtil.execSql(isdmsOracleConn, delSql);
			
			//------------临时-------------------
//			Map<String,Object> map = new HashMap<String,Object>();
//			map.put("day", yesterday);
//			map.put("source_type","工业互联网");
//			map.put("source_name","工业互联网");
//			map.put("file_size", 5236);
//			map.put("count", 121028829);
//			resList.add(map);
//			map = new HashMap<String,Object>();
//			map.put("day", yesterday);
//			map.put("source_type","网安");
//			map.put("source_name","网安");
//			map.put("file_size", 5);
//			map.put("count", 824332);
//			resList.add(map);
			//------------------------------
			
			insertSql = "insert into t_st_source_statistics(source_type,source_name,count,file_size,day) values (?,?,?,?,?)";
			columns = "source_type,source_name,count,file_size,day";
			JdbcUtil.insertBatchTableData(isdmsOracleConn, resList, insertSql, columns, 500);
			
			log.info("=========数据汇总完成==========");
		}catch(Exception e) {
			log.info("=========数据汇总异常==========");
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
		}finally {
			JdbcUtil.close(isdmsOracleConn);
			JdbcUtil.close(isdmsMysqlConn);
		}
	}

	/**
	 * 统计前天数据
	 * @param isdmsMysqlConn
	 * @param sqlInfo
	 * @param query_day_before2
	 * @param map
	 */
	private static void getDayBefore2(Connection isdmsMysqlConn, SqlInfo sqlInfo, String query_day_before2,Map<String, Object> map) {
		String source_name = CommonUtils.valueOf(map.get("source_name"));
		String table_name = "";
		String sqlRes = "";
		if(StringUtils.equals(source_name, "工业互联网")) {
			sqlRes = SqlParser.getSql(sqlInfo.getSqlIndustryInternet(), "query_day", query_day_before2+"%");
		}else if(StringUtils.equals(source_name, "网安")) {
			sqlRes = SqlParser.getSql(sqlInfo.getSqlNetsafety(), "query_day", query_day_before2+"%");
		}else {
			if(StringUtils.equals(source_name, "idc")) {
				table_name = "t_ods_idc_%";
			}else if(StringUtils.equals(source_name, "dns")) {
				table_name = "t_ods_dns_%";
			}else if(StringUtils.equals(source_name, "云")) {
				table_name = "t_ods_ircs_%";
			}else if(StringUtils.equals(source_name, "cdn")) {
				table_name = "t_ods_cdn_%";
			}
			sqlRes = SqlParser.getSql(sqlInfo.getSqlCommRes(), "table_name", table_name);
			sqlRes = SqlParser.getSql(sqlRes, "query_day", query_day_before2+"%");
		}
		List<Map<String,Object>> resCommList = getList(isdmsMysqlConn,sqlRes);
		if(resCommList != null && resCommList.size() > 0) {
			Map<String, Object> datas = resCommList.get(0);
			map.put("count",datas.get("count") == null ? 0 : datas.get("count"));
			map.put("file_size",datas.get("file_size") == null ? 0 : datas.get("file_size"));
		}
	}
	
	public static String getCount(Connection conn,String sql) {
		String count = "0";
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			
			List<Map<String,Object>> list = JdbcUtil.resultSetToList(rs);
			if(list != null && list.size() > 0) {
				String countStr = CommonUtils.valueOf(list.get(0).get("COUNT"));
				if(StringUtils.isNotBlank(countStr)) {
					count = countStr;
				}
			}
		} catch (SQLException e) {
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
		}finally {
			JdbcUtil.close(rs);
			JdbcUtil.close(ps);
		}
		return count;
	}
	
	public static List<Map<String,Object>> getList(Connection conn,String sql) {
		List<Map<String,Object>> list = new ArrayList<Map<String, Object>>();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			
			list = JdbcUtil.resultSetToList(rs);
		} catch (SQLException e) {
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
		}finally {
			JdbcUtil.close(rs);
			JdbcUtil.close(ps);
		}
		return list;
	}
	
	/**
	 * 获取配置文件中的信息
	 * @return
	 */
	public static SqlInfo getSqlInfo() {
		SqlInfo sql = null;
		try {
			XStream xstream = new XStream(new DomDriver());
			xstream.processAnnotations(SqlInfo.class);
			ClassLoader classLoader = StatisticsTask.class.getClassLoader();  
			String xml = IOUtils.toString(classLoader.getResourceAsStream("sqlconfig/sqlInfo.xml"),"UTF-8"); 
			sql = (SqlInfo) xstream.fromXML(xml);
		}catch(Exception e) {
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
		}
		return sql;
	}
	
	public static String getHiveCount(Connection conn,String sql) {
		String count = "0";
		PreparedStatement ppst = null;
		ResultSet rs = null;
		try {
			ppst = conn.prepareStatement(sql);
			rs = ppst.executeQuery();
			if(rs.next()) {
				count = rs.getString("count");
			}
		} catch (Exception e) {
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
		}finally {
			JdbcUtil.close(rs);
			JdbcUtil.close(ppst);
		}
		return count;
	}
	
	public static boolean isValidDate(String date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		try{
			sdf.setLenient(false);//严格按照给定的格式来校验
			sdf.parse(StringUtils.trim(date));
			return true;
		}catch(Exception e){
			log.error("=========传入的日期错误,正确格式为【yyyyMMdd】且是有效日期=========");
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
			return false;
		}
	}
}